class EventEmit {
  constructor () {
    this.events = {
      // haha: [handler, handler, handler...],
      // foo: []
    }
  }

  on (eventName, handler) {
    // 有不同的事件类型
    // 相同的事件类型可以注册多次处理函数
    const handlers = this.events[eventName]
    if (handlers) {
      handlers.push(handler)
    } else {
      this.events[eventName] = []
      this.events[eventName].push(handler)
    }
  }

  emit (eventName, ...args) {
    const handlers = this.events[eventName]
    if (handlers) {
      handlers.forEach(cb => {
        cb(...args)
      })
    }
  }
}

const em = new EventEmit()

em.on('haha', message => {
  console.log(message + '1')
})

em.on('haha', message => {
  console.log(message + '2')
})

em.on('foo', message => {
  console.log(message)
})

console.log(em)

em.emit('haha', 'world')
em.emit('foo', 'hello world')
